# -*-coding:utf-8 -*-

from libs.handler import BaseHandler
from libs.sae_fts import SaeFts
from tornado.escape import json_decode

class Test(BaseHandler):
    def get(self):
        data = self.db.query("SELECT id,title,price,group_id FROM items WHERE group_id=4 and is_search=0 limit 2000");
        for val in data:
            op = {}
            op["file_mark"] = self.getMark(val["price"])
            op["file_long1"] = val["id"]

            data = SaeFts().addDoc(str(val["id"]), str(val["title"].encode("utf-8")), str(val["group_id"]), op)
            data = json_decode(data)
            
            if data.has_key("errmsg"):
                if data["errmsg"]=="success":
                    self.db.execute("UPDATE items SET is_search=1 WHERE id=%s", val["id"])
                    self.write(str(self.getMark(val["price"]))+"<br/>")

            self.write(str(data))
    
    def getMark(self,price):
        price = float(price)
        if price<50.00:
            return 9
        elif price>50.00 and price<100.00:
            return 10
        elif price>100.00 and price<300.00:
            return 11
        elif price>300.00:
            return 12

class Isno(BaseHandler):
    def get(self):
        isno = self.get_argument("k")
        data = SaeFts().search(isno.encode("utf-8"), "file_long1", "asced", 20, 0, option = {"file_mark":1})
        self.write(str(data))

handlers = []
handlers = [(r"/test",Test),
        (r"/test/isno",Isno),
        ]
